package com.bsoft.gol.hbs.bbpmessagel.template;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

import java.lang.reflect.Type;
import java.util.Iterator;

public class WxMpTemplateMessageGsonAdapter implements JsonSerializer<WxMpTemplateMessage> {

	public WxMpTemplateMessageGsonAdapter() {
	}

	@Override
	public JsonElement serialize(WxMpTemplateMessage message, Type typeOfSrc, JsonSerializationContext context) {
		JsonObject messageJson = new JsonObject();
		messageJson.addProperty("touser", message.getToUser());
		messageJson.addProperty("template_id", message.getTemplateId());
		if (message.getUrl() != null) {
			messageJson.addProperty("url", message.getUrl());
		}

		JsonObject data;
		if (message.getMiniProgram() != null) {
			data = new JsonObject();
			data.addProperty("appid", message.getMiniProgram().getAppid());
			data.addProperty("pagepath", message.getMiniProgram().getPagepath());
			messageJson.add("miniprogram", data);
		}

		data = new JsonObject();
		messageJson.add("data", data);

		WxMpTemplateData datum;
		JsonObject dataJson;
		for (Iterator var6 = message.getData().iterator(); var6.hasNext(); data.add(datum.getName(), dataJson)) {
			datum = (WxMpTemplateData) var6.next();
			dataJson = new JsonObject();
			dataJson.addProperty("value", datum.getValue());
			if (datum.getColor() != null) {
				dataJson.addProperty("color", datum.getColor());
			}
		}

		return messageJson;
	}
}
